<?php
namespace Baseapp\Models;

use Phalcon\Db\Column;

class Master extends \Phalcon\Mvc\Model
{

    public $id;

    public $group_id;

    public $username;

    public $password;

    public $nickname;

    public $realname;

    public $email;

    public $is_del;

    public $add_time;

    public static $lockStatusOptions = array(
        0 => '正常',
        1 =>'已锁定'
    );

    public static $genderOptions = array(
        0 => '保密',
        1 => '男',
        2 =>'女'
    );


    public function initialize()
    {

        $this->belongsTo("group_id", "MasterGroup", "id");
    }

    public static function getSearchResult($filters = '')
    {

        $conditions = '';
        $parameters = array();

        if (!empty($filters['realname'])) {
            $conditions .= " realname = :realname: AND";
            $parameters['realname'] = $filters['realname'];
        }

        if (!empty($filters['username'])) {
            $conditions .= " username = :username: AND ";
            $parameters['username'] = $filters['username'];
        }

         if (!empty($filters['email'])) {
            $conditions .= " email = :email: AND";
            $parameters['email'] = $filters['email'];
        }

        if ($filters['group_id'] > 0) {
            $conditions .= " group_id = :group_id: AND";
            $parameters['group_id'] = $filters['group_id'];
        }

        if ($filters['is_lock'] > -1) {
            $conditions .= " is_lock = :is_lock: AND";
            $parameters['is_lock'] = $filters['is_lock'];
        }

        if (isset($filters['is_del'])) {
            $conditions .= " is_del = :is_del: AND";
            $parameters['is_del'] = $filters['is_del'];
        }

        $conditions = rtrim($conditions, 'AND');
        //echo $conditions;exit;
        //$conditions = "username = :username: AND is_del = :is_del:";

        /*
        //Parameters whose keys are the same as placeholders
        $parameters = array(
            "username" => "admin",
            "is_del" => 0
        );*/

        //Perform the query
        return Master::find(array(
            $conditions,
            "bind" => $parameters
        ));

        /*
        $masters = Master::find(array("order" => "id"));


        //Casting Types
        $types = array(
            "realname" => Column::BIND_PARAM_STR,
            //"username" => Column::BIND_PARAM_STR,
            //"email" => Column::BIND_PARAM_STR,
            //"is_del" => Column::BIND_PARAM_INT
        );

        $queryStr = '';
        foreach ($parameters as $k => $v) {
            $queryStr .= "{$k} = :{$k}: AND ";
        }
        $queryStr = rtrim($queryStr, 'AND ');
        //echo $queryStr;exit;
        // Query robots binding parameters with string placeholders
        return Robots::find(array(
            $queryStr,
            "bind" => $parameters,
            "bindTypes" => $types
        ));*/
    }
}
